<template>
  <div class="header">
    <div class="page-title">工作人员管理</div>
    <el-button
      class="m-t-15"
      type="primary"
      size="small"
      icon="el-icon-edit"
      @click="handleCCreate"
      >添加人员</el-button
    >
    <el-table :data="tableData" class="m-t-5">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id" width="100"></el-table-column>
      <el-table-column prop="name" label="姓名" width="100"></el-table-column>
      <el-table-column
        prop="phone"
        label="手机号"
        width="300"
      ></el-table-column>
      <el-table-column
        prop="phone"
        label="紧急电话"
        width="300"
      ></el-table-column>
      <el-table-column
        prop="department"
        label="部门"
        width="300"
      ></el-table-column>
      <el-table-column label="操作" min-width="200">
        <template #default="v">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            class="blue-btn"
            @click="handleEdit(v.row)"
            >编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(v.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      hide-on-single-page
      layout="prev, pager, next"
      :total="total"
      :current-page="current"
      @current-change="changePage"
    >
    </el-pagination>
  </div>
  <el-dialog title="人员操作" v-model="dialogFormVisible" width="35%">
   
    <el-form>
      <el-form-item label="姓名">
        <el-input
          v-model="form.name"
          autocomplete="off"
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input
          v-model="form.phone"
          autocomplete="off"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="紧急手机号">
        <el-input
          v-model="form.phone"
          autocomplete="off"
          placeholder="请输入紧急手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门">
        <el-input
          v-model="form.department"
          autocomplete="off"
          placeholder="请输入部门"
        ></el-input>
      </el-form-item>
    </el-form>

    <template v-slot:footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmitForm">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { post } from "@/network/request";

export default {
  name: "SeekType",
  data() {
    return {
      tableData: [
        { id: 1, department: "研发部", name: "张三", phone: "18835188888" },
        { id: 1, department: "研发部", name: "张三", phone: "18835188888" },
        { id: 1, department: "研发部", name: "张三", phone: "18835188888" },
        { id: 1, department: "研发部", name: "张三", phone: "18835188888" },
        { id: 1, department: "研发部", name: "张三", phone: "18835188888" },
      ],
      current: 1,
      total: 0,
      form: [],
      dialogFormVisible: false,
      actionType: 0, // 1 添加  2 编辑
    };
  },
  computed: {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.$nextTick(() => {
        post("/card/getVipcards", {})
          .then((res) => {
            const result = res.data;
            if (result.code == 200) {
              this.tableData = result.data.data;
              this.total = result.data.total;
              this.current = result.data.current;
            }
          })
          .catch();
      });
    },
    // 添加
    handleCCreate() {
      this.actionType = 1;
      this.form = [];
      this.dialogFormVisible = true;
    },
    // 编辑
    handleEdit(row) {
      this.actionType = 2;
      this.form = row;
      this.dialogFormVisible = true;
    },
    // 提交
    handleSubmitForm() {
      let apiAddr = "";
      switch (this.actionType) {
        case 1: // 添加
          apiAddr = "checks/create";
          break;
        case 2: // 编辑
          apiAddr = "checks/update";
          break;
        default:
          this.$message.error({
            message: "操作类型不正确",
            offset: 70,
          });
          return false;
      }
      post(apiAddr, {
        ...this.form,
      }).then((res) => {
        const result = res.data;
        if (result.code == 200) {
          this.dialogFormVisible = false;
          this.$notify.success({
            title: "系统提示",
            message: result.message,
            offset: 70,
          });
          this.changePage(1);
        } else {
          this.$message.error({
            message: result.message,
            offset: 70,
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除，是否继续？", "系统提示", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          post("/card/deleteAtcard", {
            id: row.id,
          })
            .then((res) => {
              const result = res.data;
              if (result.code == 200) {
                this.$message.success({
                  message: "删除成功！",
                  offset: 70,
                });
              } else {
                this.$message.error({
                  message: result.message,
                  offset: 70,
                });
              }
            })
            .catch();
        })
        .catch(() => {
          this.$message.info({
            message: "已取消删除！",
            offset: 70,
          });
        });
    },
    // 分页
    changePage(page) {
      post("/card/getVipcards", {
        page,
      })
        .then((res) => {
          const result = res.data;
          if (result.code == 200) {
            this.tableData = result.data.data;
            this.total = result.data.total;
            this.current = result.data.current;
          }
        })
        .catch();
    },
  },
};
</script>

<style scoped  lang="less">
</style>